﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.IO;


namespace ADayRoiAdmin
{
       
    public partial class frmPersonalSales : Form
    {  static SqlConnection con = null;
        private string temp_picThumb = string.Empty, temp_picSummary = String.Empty;
        AdayroiClass adayroi = new AdayroiClass(ref con);
        int product_id = 0, parent = 0;
        DataTable dt_price = null;
        DataRow dr_product = null;
      private BindingSource bm = new BindingSource();
       
        public frmPersonalSales(int productid, DataRow dr_product,int parent)
        {
            InitializeComponent();
            this.dr_product = dr_product;
            this.product_id = productid;
            this.parent = parent;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            string picture = adayroi.get_path_pic();
            //temp_picSummary = adayroi.loadpicture(picture, picSummary);
        }

        private void frmPersonalSales_Load(object sender, EventArgs e) 
        {
           
            PriceDataGrid();
            try
            {
                DataTable dt_Category2 = adayroi.getList("select c.PKID, c.Name from Category c where Parent=" + parent + "and IsDisplay='True' order by Sort", con);
                cb_CategoryID2.DataSource = dt_Category2;
                cb_CategoryID2.DisplayMember = "Name";
                cb_CategoryID2.ValueMember = "PKID";
            }
            catch { }

            if (product_id > 0)
            {
                cb_CategoryID2.SelectedValue = dr_product["CategoryID"].ToString();
                txt_ProductCode.Text = dr_product["ProductCode"].ToString();
                txt_ProductName.Text = dr_product["Title"].ToString();
                txt_Username.Text = dr_product["Username"].ToString();
                txt_Description.Text = dr_product["Description"].ToString();
                txt_DoUuTien.Text = dr_product["Sort"].ToString();
                chk_hienthi.Checked = (bool)dr_product["isDisplay"];
                isVip.Checked = (bool)dr_product["isVIP"];
            }
            else
                {
                    txt_DoUuTien.Text = "1";
            }
     
           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
           
            string sql = "";

           
                if (product_id == 0)
                {
                    sql = "insert into PersonalSales (ProductCode,Title,Username, Sort, isDisplay, CategoryID,Createdon,Description, IsVIP) values ('"
                            + txt_ProductCode.Text + "',N'"
                            + txt_ProductName.Text +"','"
                            + txt_Username.Text+"','"
                            + txt_DoUuTien.Text+"','"
                            + chk_hienthi.Checked+"','"
                            + cb_CategoryID2.SelectedValue+"','"
                            + DateTime.Now.ToString()+"','"
                            + txt_Description.Text + "','"
                            + isVip.Checked+                                                                          
                            "')";

                }
                else
                {
                    sql = "Update PersonalSales set  ProductCode ='" + txt_ProductCode.Text
                        + "', Title =  N'" + txt_ProductName.Text
                        +"',Username = '"+txt_Username.Text 
                        +"',Sort = '"+txt_DoUuTien.Text
                        + "',isDisplay='"+chk_hienthi.Checked
                        +"',CategoryID = '"+cb_CategoryID2.SelectedValue
                        +"' ,Updatedon ='"+DateTime.Now.ToString()
                        +"',Description ='"+txt_Description.Text
                        +"',isVIP='"+isVip.Checked
                        + "' where PKID = " + product_id.ToString();
                        
                }
                con.Open();
                SqlTransaction trans = con.BeginTransaction();
                if (adayroi.UpdateSQL(con, ref trans, sql) == 0)
                {
                    if (product_id == 0)
                    {
                        DataTable dt_product_max = adayroi.getList(" select max(PKID) from PersonalSales ", con);
                        product_id = Convert.ToInt32(dt_product_max.Rows[0][0]);
                    }
                    if (upDatePrice(ref trans) == 0)
                    {
                        MessageBox.Show("THanh cong");
                        trans.Commit();
                        con.Close();
                        dt_price = adayroi.getList("select PKID,PersonalSalesID,PriceActual,PricePromotion,SinceDate from Prices where PersonalSalesID = " + product_id, con);
                        bm.DataSource = dt_price;
                        dgv_Price.DataSource = bm;
                        dr_product["Title"] = txt_ProductName.Text;
                        dr_product["ProductCode"] = txt_ProductCode.Text;
                        dr_product["Username"] = txt_Username.Text;
                        dr_product["Sort"] = txt_DoUuTien.Text;
                        dr_product["isDisplay"] = chk_hienthi.Checked;
                        dr_product["CategoryID"] = cb_CategoryID2.SelectedValue;
                        dr_product["Description"] = txt_Description.Text;
                        dr_product["isVIP"] = isVip.Checked;
                    }
                    else
                    {
                        trans.Rollback();
                        con.Close();
                    }
                
                }
                else
                    MessageBox.Show("That bai");
            
        }
      
       
        private void btn_Cancel_Click(object sender, EventArgs e)
        {

            this.Close();
        }

        private void txt_DoUuTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            adayroi.key_press(sender, e);
        }

        private void AddControlGrid()
        {

            DataGridViewTextBoxColumn LoadPrice = new DataGridViewTextBoxColumn();
            LoadPrice.DataPropertyName = "PriceActual";
            LoadPrice.Name = "PriceActual";
            LoadPrice.HeaderText = "Giá gốc";

            DataGridViewTextBoxColumn LoadPriceLast = new DataGridViewTextBoxColumn();
            LoadPriceLast.DataPropertyName = "PricePromotion";
            LoadPriceLast.Name = "PricePromotion";
            LoadPriceLast.HeaderText = "Giá còn lại";

            DataGridViewTextBoxColumn PersonalSalesId = new DataGridViewTextBoxColumn();
            PersonalSalesId.DataPropertyName = "PersonalSalesID";
            PersonalSalesId.Name = "PersonalSalesID";
            PersonalSalesId.HeaderText = "PersonalSalesID";
            PersonalSalesId.Visible = false;

            DataGridViewTextBoxColumn Pkid = new DataGridViewTextBoxColumn();
            Pkid.DataPropertyName = "PKID";
            Pkid.Name = "PKID";
            Pkid.HeaderText = "PKID";
            Pkid.Visible = false;

            DataGridViewTextBoxColumn _SinceDate = new DataGridViewTextBoxColumn();
            _SinceDate.DataPropertyName = "SinceDate";
            _SinceDate.Name = "SinceDate";
            _SinceDate.HeaderText = "SinceDate";
            _SinceDate.Visible = false;

            dgv_Price.Columns.AddRange(new DataGridViewColumn[] { LoadPrice, LoadPriceLast, PersonalSalesId, Pkid, _SinceDate });
        }

        private void PriceDataGrid()
        {
            dt_price = adayroi.getList("select PKID, PriceActual, PricePromotion, PersonalSalesID,SinceDate from Prices where PersonalSalesID =" + product_id, con);
            AddControlGrid();
            bm.DataSource = dt_price;
            dgv_Price.DataSource = bm;

            //dgv_Price.DataSource = dt_price;
        }

        public DataRow _dr_product
        {
            get { return dr_product; }
        }

        private void dgv_Price_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            int colcurrent = this.dgv_Price.CurrentCell.ColumnIndex;
            string colheader = dgv_Price.Columns[colcurrent].Name;

            if (e.Control is TextBox)
            {
                TextBox tb = e.Control as TextBox;
                tb.KeyPress += new KeyPressEventHandler(adayroi.key_press);
            }
         
        }
        private int upDatePrice(ref SqlTransaction trans)
        {
            int maloi = 0;
            dt_price = (DataTable)bm.DataSource;
            foreach (DataRow dr in dt_price.Rows)
            {
                if (dr.RowState == DataRowState.Deleted)
                {
                    int pkid = (int)dr["PKID", DataRowVersion.Original];
                    maloi = adayroi.UpdateSQL(con,ref trans, " delete Prices where PKID = " + pkid);
                }
                else
                    if (dr.RowState == DataRowState.Modified)
                    {
                        maloi = adayroi.UpdateSQL(con,ref trans, " update Prices set PriceActual = " + dr["PriceActual"].ToString() + ", PricePromotion = " +
                            dr["PricePromotion"].ToString() + ", SinceDate = '" + dr["SinceDate"].ToString() + "' where PKID = " + dr["PKID"].ToString());

                    }
                    else if (dr.RowState == DataRowState.Added)
                    {
                        maloi = adayroi.UpdateSQL(con, ref trans," insert into Prices (PriceActual,PricePromotion,PersonalSalesID,SinceDate) values (" + dr["PriceActual"].ToString() +
                            ", " + dr["PricePromotion"].ToString() + "," + product_id + ", '" + dr["SinceDate"].ToString() + "')");
                    }

            }
          
            return maloi;
        }

        private void btn_Image_Click(object sender, EventArgs e)
        {
            fmImages image = new fmImages(product_id, "PersonalSalesID");
            image.ShowDialog();
        }
    }
}
